package com.meiyu.live.widget;

import android.content.Context;
import android.content.Intent;
import android.os.CountDownTimer;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.android.volley.Response;
import com.meiyu.live.BaseActivity;
import com.meiyu.live.R;
import com.meiyu.live.comm.Constant;
import com.meiyu.live.comm.UserConfig;
import com.meiyu.live.eventbus.DealEvent;
import com.meiyu.live.eventbus.RechargeEvent;
import com.meiyu.live.eventbus.WinEvent;
import com.meiyu.live.eventbus.YazhuEvent;
import com.meiyu.live.net.param.BaseBodyParams;
import com.meiyu.live.net.param.BetParams;
import com.meiyu.live.net.param.GetPageParams;
import com.meiyu.live.net.param.NiuIdParams;
import com.meiyu.live.net.response.DealResponse;
import com.meiyu.live.net.response.HistoryResponse;
import com.meiyu.live.net.response.OpenResponse;
import com.meiyu.live.popj.NiuHistory;
import com.meiyu.live.popj.OpenResult;
import com.meiyu.live.ui.mine.RechargeActivity;
import com.meiyu.live.util.ToastUtil;

import java.util.ArrayList;
import java.util.List;

import de.greenrobot.event.EventBus;

/**
 * Created by LI on 2017/5/14.
 */

public class GamePopupwind extends FrameLayout implements View.OnClickListener, CompoundButton.OnCheckedChangeListener {
    /**
     * Created by Administrator on 2017/5/2.
     */
    private RecyclerView listView;
    private ImageView imageView_fir, imageView_s, imageView_t, imageView_fo, imageView_fiv;
    private ImageView simageView_fir, simageView_s, simageView_t, simageView_fo, simageView_fiv;
    private ImageView timageView_fir, timageView_s, timageView_t, timageView_fo, timageView_fiv;
    private ImageView fapai, close_game, histroy, time_icon, xiala_close, iv_cz;
    private View left_view;
    private ImageView niu_fir, niu_sen, niu_thr;
    private TextView b, time, yazhu_f, yazhu_s, yazhu_t, tv_coins;
    private TranslateAnimation animation;
    private int i = 0;
    private int coins = 0;
    private long niu_sid;

    private List<NiuHistory> list = new ArrayList<>();
    private HistoryAdapter adapter;
    /**
     * 切换倍数
     */
    private int xiazu_money = 100;
    private RadioGroup radiogroup;
    private RadioButton radio1, radio2, radio3;
    private CheckBox cb1, cb2, cb3;
    private LinearLayout cb1_layout, cb2_layout, cb3_layout, bg_layout, xiazhu_layout, yazhu_layout;
    /**
     * 下注倍数
     */
    private int first_multiple, sencond_multiple, third_multiple;
    /**
     * 下注底金
     */
    private int first_money, sencond_money, third_money;
    private BaseActivity activity;
    private int time_param = 15000;
    private CountDownTimer cdt;
    private int userType = 2;

    OnHideListener onHideListener;

    public GamePopupwind(Context context) {
        super(context);
        this.activity = (BaseActivity) context;
        LayoutInflater.from(activity).inflate(R.layout.poup_animation, this);
    }

    public GamePopupwind(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.activity = (BaseActivity) context;
        LayoutInflater.from(activity).inflate(R.layout.poup_animation, this);
    }

    public void setOnHideListener(OnHideListener onHideListener) {
        this.onHideListener = onHideListener;
    }

    public interface OnHideListener {
        void onHide();
    }

    public void init(int userType) {
        initView();
        setUserType(userType);
        beforeOrAfterShowPoker(GONE);
    }

    public void setUserType(int userType) {
        this.userType = userType;
        if (userType == 1) {
            histroy.setVisibility(View.GONE);
            time_icon.setVisibility(View.GONE);
            time.setVisibility(View.GONE);
        } else if (userType == 2) {
            fapai.setVisibility(View.GONE);
            left_view.setVisibility(View.GONE);
        }
    }


    /**
     * 初始化 UI
     */
    private void initView() {
        listView = (RecyclerView) findViewById(R.id.history_list);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(activity, LinearLayoutManager.VERTICAL, false);
        listView.setLayoutManager(linearLayoutManager);
        adapter = new HistoryAdapter();
        listView.setAdapter(adapter);
        left_view = findViewById(R.id.closegame_leftview);
        close_game = (ImageView) findViewById(R.id.closegame);
        close_game.setOnClickListener(this);
        histroy = (ImageView) findViewById(R.id.history);
        histroy.setOnClickListener(this);
        xiala_close = (ImageView) findViewById(R.id.put_down);
        xiala_close.setOnClickListener(this);
        iv_cz = (ImageView) findViewById(R.id.iv_cz);
        iv_cz.setOnClickListener(this);

        tv_coins = (TextView) findViewById(R.id.tv_coins);

        //背景图片
        bg_layout = (LinearLayout) findViewById(R.id.bg);
        //下注layout
        xiazhu_layout = (LinearLayout) findViewById(R.id.xiazhu_layout);
        //all押注
        yazhu_layout = (LinearLayout) findViewById(R.id.all_yazhu_numb);
        yazhu_f = (TextView) findViewById(R.id.first_all_yazhu_numb);
        yazhu_s = (TextView) findViewById(R.id.sencond_all_yazhu_numb);
        yazhu_t = (TextView) findViewById(R.id.third_all_yazhu_numb);
        //显示的牛牛
        niu_fir = (ImageView) findViewById(R.id.niu_one);
        niu_sen = (ImageView) findViewById(R.id.niu_two);
        niu_thr = (ImageView) findViewById(R.id.niu_three);

        //倒计时显示
        time = (TextView) findViewById(R.id.time_long);
        time_icon = (ImageView) findViewById(R.id.time);

        fapai = (ImageView) findViewById(R.id.fapai);
        fapai.setOnClickListener(this);
        //开始下注
        cb1 = (CheckBox) findViewById(R.id.checkbox_one);
        cb2 = (CheckBox) findViewById(R.id.checkbox_two);
        cb3 = (CheckBox) findViewById(R.id.checkbox_three);
        cb1.setOnCheckedChangeListener(this);
        cb2.setOnCheckedChangeListener(this);
        cb3.setOnCheckedChangeListener(this);
        cb1_layout = (LinearLayout) findViewById(R.id.first_xiazhu);
        cb2_layout = (LinearLayout) findViewById(R.id.sencond_xiazhu);
        cb3_layout = (LinearLayout) findViewById(R.id.third_xiazhu);
        cb1_layout.setOnClickListener(this);
        cb2_layout.setOnClickListener(this);
        cb3_layout.setOnClickListener(this);

        //每注钻石
        radiogroup = (RadioGroup) findViewById(R.id.rg_xiazhu_money);
        radio1 = (RadioButton) findViewById(R.id.rb1);
        radio2 = (RadioButton) findViewById(R.id.rb2);
        radio3 = (RadioButton) findViewById(R.id.rb3);

        radiogroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if (checkedId == radio1.getId()) {
                    xiazu_money = 100;
                    Log.e("fdddddd", xiazu_money + "");
                } else if (checkedId == radio2.getId()) {
                    xiazu_money = 1000;
                    Log.e("fdddddd", xiazu_money + "");
                } else if (checkedId == radio3.getId()) {
                    xiazu_money = 10000;
                    Log.e("fdddddd", xiazu_money + "");
                }
            }
        });
        //第一个庄家
        imageView_fir = (ImageView) findViewById(R.id.imageView_first);
        imageView_s = (ImageView) findViewById(R.id.imageView_sencond);
        imageView_t = (ImageView) findViewById(R.id.imageView_third);
        imageView_fo = (ImageView) findViewById(R.id.imageView_fource);
        imageView_fiv = (ImageView) findViewById(R.id.imageView_five);
        imageView_fir.setOnClickListener(onClickListener1);
        imageView_s.setOnClickListener(onClickListener1);
        imageView_t.setOnClickListener(onClickListener1);
        imageView_fo.setOnClickListener(onClickListener1);
        imageView_fiv.setOnClickListener(onClickListener1);

        //第二个庄家
        simageView_fir = (ImageView) findViewById(R.id.simageView_first);
        simageView_s = (ImageView) findViewById(R.id.simageView_sencond);
        simageView_t = (ImageView) findViewById(R.id.simageView_third);
        simageView_fo = (ImageView) findViewById(R.id.simageView_fource);
        simageView_fiv = (ImageView) findViewById(R.id.simageView_five);
        simageView_fir.setOnClickListener(onClickListener2);
        simageView_s.setOnClickListener(onClickListener2);
        simageView_t.setOnClickListener(onClickListener2);
        simageView_fo.setOnClickListener(onClickListener2);
        simageView_fiv.setOnClickListener(onClickListener2);

        //第三个庄家
        timageView_fir = (ImageView) findViewById(R.id.timageView_first);
        timageView_s = (ImageView) findViewById(R.id.timageView_sencond);
        timageView_t = (ImageView) findViewById(R.id.timageView_third);
        timageView_fo = (ImageView) findViewById(R.id.timageView_fource);
        timageView_fiv = (ImageView) findViewById(R.id.timageView_five);
        timageView_fir.setOnClickListener(onClickListener3);
        timageView_s.setOnClickListener(onClickListener3);
        timageView_t.setOnClickListener(onClickListener3);
        timageView_fo.setOnClickListener(onClickListener3);
        timageView_fiv.setOnClickListener(onClickListener3);

        hideHoper();

        animation = new TranslateAnimation(800, 0, 0, 0);
        animation.setDuration(80);//设置动画持续时间
        animation.setRepeatCount(0);//设置重复次数
//        animation.setRepeatMode(Animation.REVERSE);//设置反方向执行
        animation.setFillAfter(true);
        animation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
                i++;
                displayImgHoper(i);
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                clearAndStartAnimation(i);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
    }

    private void getHistory() {
        int area[] = {100, 100};
        GetPageParams pageParams = new GetPageParams(area);
        pageParams.setUrl(Constant.UrlParams.GAME_QUERY_HIS);
        //热门约播取hot_hook_ups字段的值
        activity.request(pageParams, HistoryResponse.class, new Response.Listener<HistoryResponse>() {
            @Override
            public void onResponse(HistoryResponse response) {
                if (response.getStatusCode() == 6) {
                    list.clear();
                    list.addAll(response.getRemark());
                    adapter.notifyDataSetChanged();
                }

            }
        });
    }

    public void setCoins(int coins) {
        this.coins = coins;
        tv_coins.setText(coins + "");
    }

    private void timeoverViewNotclick() {
        cb1_layout.setClickable(false);
        cb2_layout.setClickable(false);
        cb3_layout.setClickable(false);
        cb1.setClickable(false);
        cb2.setClickable(false);
        cb3.setClickable(false);
        imageView_fir.setClickable(false);
        imageView_s.setClickable(false);
        imageView_t.setClickable(false);
        imageView_fo.setClickable(false);
        imageView_fiv.setClickable(false);

        simageView_fir.setClickable(false);
        simageView_s.setClickable(false);
        simageView_t.setClickable(false);
        simageView_fo.setClickable(false);
        simageView_fiv.setClickable(false);

        timageView_fir.setClickable(false);
        timageView_s.setClickable(false);
        timageView_t.setClickable(false);
        timageView_fo.setClickable(false);
        timageView_fiv.setClickable(false);
    }

    private void timeoverViewOnclick() {
        cb1_layout.setClickable(true);
        cb2_layout.setClickable(true);
        cb3_layout.setClickable(true);
        cb1.setClickable(true);
        cb2.setClickable(true);
        cb3.setClickable(true);

        imageView_fir.setClickable(true);
        imageView_s.setClickable(true);
        imageView_t.setClickable(true);
        imageView_fo.setClickable(true);
        imageView_fiv.setClickable(true);

        simageView_fir.setClickable(true);
        simageView_s.setClickable(true);
        simageView_t.setClickable(true);
        simageView_fo.setClickable(true);
        simageView_fiv.setClickable(true);

        timageView_fir.setClickable(true);
        timageView_s.setClickable(true);
        timageView_t.setClickable(true);
        timageView_fo.setClickable(true);
        timageView_fiv.setClickable(true);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.fapai:
                fapai.setClickable(false);
                if (userType == 1) {
                    activity.request(new BaseBodyParams(Constant.UrlParams.GAME_DEAL), DealResponse.class, new Response.Listener<DealResponse>() {
                        @Override
                        public void onResponse(DealResponse baseResponse) {
                            if (baseResponse.getStatusCode() == 6) {
                                niu_sid = baseResponse.getRemark().getNiu_sid();
                                close_game.setClickable(false);
                                setTimeParam(15000);
                                fapai();
                                EventBus.getDefault().post(new DealEvent());
                            } else {
                                ToastUtil.show(baseResponse.getMsg());
                            }
                        }
                    });
                }
                break;
            case R.id.first_xiazhu:
                if (cb1.isChecked()) {
                    cb1.setChecked(false);
                } else {
                    cb1.setChecked(true);
                }
                break;
            case R.id.sencond_xiazhu:
                if (cb2.isChecked()) {
                    cb2.setChecked(false);
                } else {
                    cb2.setChecked(true);
                }
                break;
            case R.id.third_xiazhu:
                if (cb3.isChecked()) {
                    cb3.setChecked(false);
                } else {
                    cb3.setChecked(true);
                }
                break;
            case R.id.closegame:
                onHideListener.onHide();
                init(userType);
                reset();
//                userFapai();
                break;
            case R.id.iv_cz:
                Intent intent = new Intent(activity, RechargeActivity.class);
                activity.startActivity(intent);
                break;
            case R.id.put_down:
                onHideListener.onHide();
                break;
            case R.id.history:
                if (listView.getVisibility() == VISIBLE) {
                    listView.setVisibility(GONE);
                } else {
                    listView.setVisibility(VISIBLE);
                    getHistory();
                }
                break;
        }
    }

    OnClickListener onClickListener1 = new OnClickListener() {
        @Override
        public void onClick(View view) {
            if (cb1.isChecked()) {
                cb1.setChecked(false);
            } else {
                cb1.setChecked(true);
            }
        }
    };

    OnClickListener onClickListener2 = new OnClickListener() {
        @Override
        public void onClick(View view) {
            if (cb2.isChecked()) {
                cb2.setChecked(false);
            } else {
                cb2.setChecked(true);
            }
        }
    };

    OnClickListener onClickListener3 = new OnClickListener() {
        @Override
        public void onClick(View view) {
            if (cb3.isChecked()) {
                cb3.setChecked(false);
            } else {
                cb3.setChecked(true);
            }
        }
    };

    private void requestBets() {
        if (first_money * first_multiple + sencond_multiple * sencond_money + third_money * third_multiple > coins) {
            ToastUtil.show("钻石不足，请充值");
        } else {
            int[] a = {first_money * first_multiple, sencond_multiple * sencond_money, third_money * third_multiple};
            activity.request(new BetParams(niu_sid, a), DealResponse.class, new Response.Listener<DealResponse>() {
                @Override
                public void onResponse(DealResponse baseResponse) {
                    if (baseResponse.getStatusCode() == 6) {
                        setYazhu(baseResponse.getBets());
                        EventBus.getDefault().post(new YazhuEvent());
                    }

                }
            });
        }
    }

    /**
     * 动画开始前站位隐藏
     */
    private void hideHoper() {
        imageView_fir.setVisibility(View.INVISIBLE);
        imageView_s.setVisibility(View.INVISIBLE);
        imageView_t.setVisibility(View.INVISIBLE);
        imageView_fo.setVisibility(View.INVISIBLE);
        imageView_fiv.setVisibility(View.INVISIBLE);

        simageView_fir.setVisibility(View.INVISIBLE);
        simageView_s.setVisibility(View.INVISIBLE);
        simageView_t.setVisibility(View.INVISIBLE);
        simageView_fo.setVisibility(View.INVISIBLE);
        simageView_fiv.setVisibility(View.INVISIBLE);

        timageView_fir.setVisibility(View.INVISIBLE);
        timageView_s.setVisibility(View.INVISIBLE);
        timageView_t.setVisibility(View.INVISIBLE);
        timageView_fo.setVisibility(View.INVISIBLE);
        timageView_fiv.setVisibility(View.INVISIBLE);
    }

    /**
     * 动画开始显示当前imag动画
     *
     * @param i 当前位置
     */
    private void displayImgHoper(int i) {
        if (i == 1) {
            imageView_fir.setVisibility(View.VISIBLE);
        } else if (i == 2) {
            imageView_s.setVisibility(View.VISIBLE);
        } else if (i == 3) {
            imageView_t.setVisibility(View.VISIBLE);
        } else if (i == 4) {
            imageView_fo.setVisibility(View.VISIBLE);
        } else if (i == 5) {
            imageView_fiv.setVisibility(View.VISIBLE);
        } else if (i == 6) {
            simageView_fir.setVisibility(View.VISIBLE);
        } else if (i == 7) {
            simageView_s.setVisibility(View.VISIBLE);
        } else if (i == 8) {
            simageView_t.setVisibility(View.VISIBLE);
        } else if (i == 9) {
            simageView_fo.setVisibility(View.VISIBLE);
        } else if (i == 10) {
            simageView_fiv.setVisibility(View.VISIBLE);
        } else if (i == 11) {
            timageView_fir.setVisibility(View.VISIBLE);
        } else if (i == 12) {
            timageView_s.setVisibility(View.VISIBLE);
        } else if (i == 13) {
            timageView_t.setVisibility(View.VISIBLE);
        } else if (i == 14) {
            timageView_fo.setVisibility(View.VISIBLE);
        } else if (i == 15) {
            timageView_fiv.setVisibility(View.VISIBLE);
        }
    }

    /**
     * 清除image动画（当前已完成）并开始显示下一个imag动画
     *
     * @param i 当前位置
     */
    private void clearAndStartAnimation(int i) {
        if (i == 1) {
            imageView_fir.clearAnimation();
            imageView_s.startAnimation(animation);
        } else if (i == 2) {
            imageView_s.clearAnimation();
            imageView_t.startAnimation(animation);
        } else if (i == 3) {
            imageView_t.clearAnimation();
            imageView_fo.startAnimation(animation);
        } else if (i == 4) {
            imageView_fo.clearAnimation();
            imageView_fiv.startAnimation(animation);
        } else if (i == 5) {
            imageView_fiv.clearAnimation();
            simageView_fir.startAnimation(animation);
        } else if (i == 6) {
            simageView_fir.clearAnimation();
            simageView_s.startAnimation(animation);
        } else if (i == 7) {
            simageView_s.clearAnimation();
            simageView_t.startAnimation(animation);
        } else if (i == 8) {
            simageView_t.clearAnimation();
            simageView_fo.startAnimation(animation);
        } else if (i == 9) {
            simageView_fo.clearAnimation();
            simageView_fiv.startAnimation(animation);
        } else if (i == 10) {
            simageView_fiv.clearAnimation();
            timageView_fir.startAnimation(animation);
        } else if (i == 11) {
            timageView_fir.clearAnimation();
            timageView_s.startAnimation(animation);
        } else if (i == 12) {
            timageView_s.clearAnimation();
            timageView_t.startAnimation(animation);
        } else if (i == 13) {
            timageView_t.clearAnimation();
            timageView_fo.startAnimation(animation);
        } else if (i == 14) {
            timageView_fo.clearAnimation();
            timageView_fiv.startAnimation(animation);
        } else if (i == 15) {
            timageView_fiv.clearAnimation();
        }
    }

    @Override
    public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
        if (compoundButton.getId() == cb1.getId()) {
            int temp = first_money;
            first_money = xiazu_money;
            if (temp == xiazu_money) {
                first_multiple++;
                Log.e("first_multiple", "first_multiple: " + first_multiple);
                if (first_multiple == 0) {
                    cb1.setText("开始下注");
                } else if (first_multiple == 1) {
                    cb1.setText(xiazu_money + "");
                } else {
                    cb1.setText(xiazu_money + "     x" + first_multiple);
                }
            } else {
                first_multiple = 1;
                cb1.setText(xiazu_money + "");
            }

            Log.e("fdddddd", first_multiple + "__1__" + (compoundButton.getId() == cb1.getId()));
        }
        if (compoundButton.getId() == cb2.getId()) {
            int temp = sencond_money;
            sencond_money = xiazu_money;
            if (temp == xiazu_money) {
                sencond_multiple++;
                if (sencond_multiple == 0) {
                    cb2.setText("开始下注");
                } else if (sencond_multiple == 1) {
                    cb2.setText(xiazu_money + "");
                } else {
                    cb2.setText(xiazu_money + "     x" + sencond_multiple);
                }
            } else {
                sencond_multiple = 1;
                cb2.setText(xiazu_money + "");
            }
            Log.e("fdddddd", sencond_multiple + "__2__" + (compoundButton.getId() == cb2.getId()));
        }
        if (compoundButton.getId() == cb3.getId()) {
            int temp = third_money;
            third_money = xiazu_money;
            if (temp == xiazu_money) {
                third_multiple++;
                if (third_multiple == 0) {
                    cb3.setText("开始下注");
                } else if (third_multiple == 1) {
                    cb3.setText(xiazu_money + "");
                } else {
                    cb3.setText(xiazu_money + "   x" + third_multiple);
                }
            } else {
                third_multiple = 1;
                cb3.setText(xiazu_money + "");
            }
            Log.e("fdddddd", third_multiple + "__3__" + (compoundButton.getId() == cb3.getId()));
        }

        requestBets();
    }

    private int xiazuMultiple(int numb) {
        int result = 0;
        result += 1;
        return result;
    }

    private void showPoker(List<String> list) {
        imageView_fir.setBackgroundResource(UserConfig.puke.get(list.get(0)));
        imageView_s.setBackgroundResource(UserConfig.puke.get(list.get(1)));
        imageView_t.setBackgroundResource(UserConfig.puke.get(list.get(2)));
        imageView_fo.setBackgroundResource(UserConfig.puke.get(list.get(3)));
        imageView_fiv.setBackgroundResource(UserConfig.puke.get(list.get(4)));

        simageView_fir.setBackgroundResource(UserConfig.puke.get(list.get(5)));
        simageView_s.setBackgroundResource(UserConfig.puke.get(list.get(6)));
        simageView_t.setBackgroundResource(UserConfig.puke.get(list.get(7)));
        simageView_fo.setBackgroundResource(UserConfig.puke.get(list.get(8)));
        simageView_fiv.setBackgroundResource(UserConfig.puke.get(list.get(9)));

        timageView_fir.setBackgroundResource(UserConfig.puke.get(list.get(10)));
        timageView_s.setBackgroundResource(UserConfig.puke.get(list.get(11)));
        timageView_t.setBackgroundResource(UserConfig.puke.get(list.get(12)));
        timageView_fo.setBackgroundResource(UserConfig.puke.get(list.get(13)));
        timageView_fiv.setBackgroundResource(UserConfig.puke.get(list.get(14)));

    }

    private void showNiu(List<String> list) {
        niu_fir.setBackgroundResource(UserConfig.niu.get(list.get(0)));
        niu_sen.setBackgroundResource(UserConfig.niu.get(list.get(1)));
        niu_thr.setBackgroundResource(UserConfig.niu.get(list.get(2)));
    }

    private void setPokerBg() {
        imageView_fir.setBackgroundResource(R.drawable.out_poker);
        imageView_s.setBackgroundResource(R.drawable.out_poker);
        imageView_t.setBackgroundResource(R.drawable.out_poker);
        imageView_fo.setBackgroundResource(R.drawable.out_poker);
        imageView_fiv.setBackgroundResource(R.drawable.out_poker);

        simageView_fir.setBackgroundResource(R.drawable.out_poker);
        simageView_s.setBackgroundResource(R.drawable.out_poker);
        simageView_t.setBackgroundResource(R.drawable.out_poker);
        simageView_fo.setBackgroundResource(R.drawable.out_poker);
        simageView_fiv.setBackgroundResource(R.drawable.out_poker);

        timageView_fir.setBackgroundResource(R.drawable.out_poker);
        timageView_s.setBackgroundResource(R.drawable.out_poker);
        timageView_t.setBackgroundResource(R.drawable.out_poker);
        timageView_fo.setBackgroundResource(R.drawable.out_poker);
        timageView_fiv.setBackgroundResource(R.drawable.out_poker);
    }

    public void beforeOrAfterShowPoker(int goneOrvisible) {
        if (goneOrvisible == View.GONE) {
            xiazhu_layout.setVisibility(View.INVISIBLE);
            niu_fir.setVisibility(goneOrvisible);
            niu_sen.setVisibility(goneOrvisible);
            niu_thr.setVisibility(goneOrvisible);
            yazhu_layout.setVisibility(View.INVISIBLE);
        } else if (goneOrvisible == View.VISIBLE) {
            xiazhu_layout.setVisibility(goneOrvisible);
            niu_fir.setVisibility(goneOrvisible);
            niu_sen.setVisibility(goneOrvisible);
            niu_thr.setVisibility(goneOrvisible);
            yazhu_layout.setVisibility(goneOrvisible);
        }
    }

    public void setYazhu(int[] bets) {
        yazhu_f.setText(bets[0] + "");
        yazhu_s.setText(bets[1] + "");
        yazhu_t.setText(bets[2] + "");
    }

    public void setNiu_sid(long niu_sid) {
        this.niu_sid = niu_sid;
    }

    //设置时间长短
    public void setTimeParam(int param) {
        reset();
        time_icon.setVisibility(View.VISIBLE);
        time.setVisibility(View.VISIBLE);
        time_param = param;
        if (userType == 2) {
            time_param += 3000;
        }
        time.setText((time_param / 1000) + " 秒");
        cdt = new CountDownTimer(time_param, 1000) {
            @Override
            public void onTick(long millisUntilFinished) {
                time.setText((millisUntilFinished / 1000) + " 秒");
                if (userType == 2) {
                    if ((millisUntilFinished / 1000) == 6) {
                        timeoverViewNotclick();

                    }
                } else {
                    if ((millisUntilFinished / 1000) == 2) {
                        timeoverViewNotclick();
                    }
                }
            }

            @Override
            public void onFinish() {
                time.setText("");
                time_icon.setVisibility(View.GONE);
                time.setVisibility(View.GONE);
                timeoverViewNotclick();
                kaipai();

                close_game.setClickable(true);
            }
        };
        cdt.start();
        xiazhu_layout.setVisibility(View.VISIBLE);
        yazhu_layout.setVisibility(View.VISIBLE);
        close_game.setClickable(false);
        fapai();
    }

    public void setPopupBg() {

        bg_layout.setBackgroundResource(R.drawable.background_open);
    }

    public void kaipai() {
        String url = Constant.UrlParams.GAME_RESULT;

        if (userType == 1) {
            url = Constant.UrlParams.GAME_OPEN;
        }


        activity.request(new NiuIdParams(url, niu_sid), OpenResponse.class, new Response.Listener<OpenResponse>() {
            @Override
            public void onResponse(OpenResponse baseResponse) {
                if (baseResponse.getStatusCode() == 6) {
                    List<String> list = new ArrayList<String>();
                    List<String> list_niu = new ArrayList<String>();
                    for (OpenResult result : baseResponse.getRemark().getResult()) {
                        for (String key : result.getPoker_group()) {
                            list.add(key);
                        }
                        list_niu.add(result.getNiu_one_top_num());
                    }
                    showPoker(list);
                    beforeOrAfterShowPoker(View.VISIBLE);
                    showNiu(list_niu);
                    EventBus.getDefault().post(new RechargeEvent());
                    if (baseResponse.getRemark().getGet_bets() > 0) {
                        EventBus.getDefault().post(new WinEvent(baseResponse.getRemark().getGet_bets()));
                    }

                } else {
                    reset();
                    ToastUtil.show(baseResponse.getMsg());
                }
                fapai.setClickable(true);
            }
        });

    }

    public void reset() {
        bg_layout.setBackground(activity.getResources().getDrawable(R.drawable.background));
        xiazu_money = 100;
        beforeOrAfterShowPoker(GONE);
        xizhuHuifu();
    }

    public void fapai() {
        i = 0;
        hideHoper();
        setPopupBg();
        setPokerBg();
        imageView_fir.startAnimation(animation);
        niu_fir.setVisibility(View.GONE);
        niu_sen.setVisibility(View.GONE);
        niu_thr.setVisibility(View.GONE);
        yazhu_f.setVisibility(VISIBLE);
        yazhu_s.setVisibility(VISIBLE);
        yazhu_t.setVisibility(VISIBLE);
        yazhu_layout.setVisibility(View.VISIBLE);
        timeoverViewOnclick();

        Log.e("fdddddd", "sssssssssssssssss");
    }

    public void userFapai() {
        i = 0;
        hideHoper();
        setPopupBg();
        setPokerBg();
        xiazhu_layout.setVisibility(View.VISIBLE);
        imageView_fir.startAnimation(animation);
        niu_fir.setVisibility(View.GONE);
        niu_sen.setVisibility(View.GONE);
        niu_thr.setVisibility(View.GONE);
        yazhu_layout.setVisibility(View.INVISIBLE);
        timeoverViewOnclick();
        Log.e("fdddddd", "sssssssssssssssss");
    }

    public void xizhuHuifu() {
        cb1.setText("开始下注");
        cb2.setText("开始下注");
        cb3.setText("开始下注");
        yazhu_f.setText("0");
        yazhu_s.setText("0");
        yazhu_t.setText("0");
        radio1.setChecked(true);
        first_multiple = 0;
        first_money = 0;
        sencond_multiple = 0;
        sencond_money = 0;
        third_multiple = 0;
        third_money = 0;
    }

    //销毁popup
    public void destory() {
        if (cdt != null) {
            cdt.cancel();
            cdt = null;
        }
    }

    public class HistoryAdapter extends
            RecyclerView.Adapter<HistoryAdapter.ViewHolder> {


        public class ViewHolder extends RecyclerView.ViewHolder {

            TextView tv_screening_id;
            TextView tv_all_bets;
            TextView tv_all_get_bets;

            public ViewHolder(View itemView) {
                super(itemView);
                tv_screening_id = (TextView) itemView.findViewById(R.id.tv_screening_id);
                tv_all_bets = (TextView) itemView.findViewById(R.id.tv_all_bets);
                tv_all_get_bets = (TextView) itemView.findViewById(R.id.tv_all_get_bets);
            }
        }

        @Override
        public int getItemCount() {
            return list.size() + 1;
        }

        @Override
        public HistoryAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
            View view = LayoutInflater.from(activity).inflate(R.layout.adapter_history,
                    viewGroup, false);
            ViewHolder viewHolder = new ViewHolder(view);
            return viewHolder;
        }

        @Override
        public void onBindViewHolder(ViewHolder holder, int position) {
            if (position != 0) {
                holder.tv_screening_id.setText(list.get(position - 1).getScreening_id());
                holder.tv_all_bets.setText(list.get(position - 1).getAll_bets());
                holder.tv_all_get_bets.setText(list.get(position - 1).getAll_get_bets());
            } else {
                holder.tv_screening_id.setText("场次编号");
                holder.tv_all_bets.setText("下注金额");
                holder.tv_all_get_bets.setText("获得钻石");
            }
        }


    }
}
